<?php

/**
 * Description of WSActiveRecord
 * <p>In WSActiveRecord you can declare and retrieve validation rules by scenario.
 * What you have to do is declare rules within methods of the form scenarioNameValidation(). 
 * Remember that when doing so, you don't have to declare 'on' => 'scenarioName' in rules() anymore.
 * <p>Common rules for all scenarios must be declared in baseValidation().
 * <p>After you have declared the rules, WSActiveRecord will append validation rules to baseValidation based on the current scenario.
 * So if you need to get rules for a scenario, just do something like: </br>
 *	$user = new User('login');</br>
 *	$loginRules = $user->rules();</br>
 * <p>NOTE that in order for this to work you must declare all your rules in baseValidation() or 
 * scenarioNameValidation() NOT in rules(). If you don't have any rules, then just don't declare anything.
 * 
 * @author Thomas Vo
 */
class BRActiveRecord extends CActiveRecord
{
	public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
 
    public static function toJSONAll($records)
    {
        $json = array();
        foreach ($records as $record) {
            $json[]= $record->toJSON();
        }
        $json = '['.implode(',', $json).']';
        return $json;
    }
    
    public function behaviors() {
        return array(
            'EJsonBehavior'=>array(
                'class'=>'application.behaviors.EJsonBehavior'
            ),
        );
    }
    public function baseValidation()
    {
        return array();
    }
    
	public function rules()
	{
		$scenario = $this->scenario;
		$baseValidation     = $this->baseValidation();
		$scenarioValidation = method_exists($this, $scenario.'Validation') ? call_user_func(array($this, $scenario.'Validation')) : array();
		return array_merge($baseValidation, $scenarioValidation);
	}
	
	
}

?>
